package net.orange.framework.validator;

import net.orange.architecture.validator.IObjectValidator;
import net.orange.framework.model.Department;

/**
 * Validation : The department code should follow the special format: 1)If this
 * is the top department, so the code is "D". 2) The department code should be
 * start with parent's code and plus two digital.
 */
public class DepartmentCodeFormatValidator extends FrameworkValidator implements
		IObjectValidator {

	/**
	 * The target department.
	 */
	private Department department;

	/**
	 * Disabled the default contractor.
	 */
	@SuppressWarnings("unused")
	private DepartmentCodeFormatValidator() throws Exception {

	}

	/**
	 * Constructor with department object to initialize the validation.
	 * 
	 * @param department
	 *            The target department.
	 * @throws Exception
	 */
	public DepartmentCodeFormatValidator(Department department)
			throws Exception {
		this.department = department;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.validator.ObjectValidator#check()
	 */
	public boolean check() throws Exception {
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.validator.ObjectValidator#getMessage()
	 */
	public String getMessage() {
		return "";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.validator.IObjectValidator#validate()
	 */
	public String validate() throws Exception {
		if (department.getParent() == null) {
			if (department.getDept_code().equals("D"))
				return null;
			else
				return message.get("error2_dept_01");
		}

		if (!department.getDept_code().startsWith(
				department.getParent().getDept_code())) {
			return message.get("error2_dept_02");
		} else {
			if (department.getDept_code().length() == department.getParent()
					.getDept_code().length()) {
				return message.get("error2_dept_03");
			}

			String s = department.getDept_code().substring(
					department.getParent().getDept_code().length(),
					department.getDept_code().length());

			if (s == null || s.length() != 2) {
				return message.get("error2_dept_03");
			}

			try {
				Integer.parseInt(s);
			} catch (Exception e) {
				return message.get("error2_dept_03");
			}
		}
		return null;
	}
}
